package com.bringway.p2p.dao.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.BxxDao;

/**
 * 
 * @author qjy
 * @version 创建时间：2016年06月14日 上午13:25:42
 * @功能：查询异常信息
 */

@Repository
public class BxxDaoImpl  extends P2PJdbcDao implements BxxDao{
	Logger logger = Logger.getLogger(BxxDaoImpl.class);
	
	
	public List<Map<String, Object>> queryBxxList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String zqid=params.optString("zqid");
		String xmt=params.optString("xmt");
		String zt=params.optString("zt");
		StringBuffer sql = new StringBuffer();
		sql.append("select t1.ZQID,t2.XM_TYPE,t2.QX_MONTH,t2.ZT, count(*) CT,max(t1.tzsj) MAX_TZSJ");
		sql.append(" ,case when t2.XM_TYPE = '01' and t2.ZT <> '1' and t2.JD = '100' then");
		sql.append("  date_format(date_add(max(t1.tzsj), interval t2.qx_month month),'%Y-%m-%d')");
		sql.append("  when t2.XM_TYPE = '02' and t2.ZT <> '1' then date_format(date_add(date_format(");
		sql.append("  date_add(t2.jxrq, interval -1 DAY), '%Y-%m-%d'), interval t2.qx_month month),'%Y-%m-%d')");
		sql.append("  end YJ_ZZ_HK_DATE from t_zhfw_tzrxx t1, t_zhfw_zqzrlb t2");
		sql.append("  where t2.HK_TYPE = '02' and t1.ZQID = t2.ID ");
		Map<String, Object> paramMap = new HashMap<>();
		if(StringUtils.isNotEmpty(zqid)){
			sql.append(" AND t1.ZQID = :zqid ");
			paramMap.put("zqid", zqid);
		}
		if(StringUtils.isNotEmpty(xmt)){
			sql.append(" AND t2.XM_TYPE = :xmt ");
			paramMap.put("xmt", xmt);
		}
		if(StringUtils.isNotEmpty(zt)){
			sql.append(" AND t2.ZT = :zt ");
			paramMap.put("zt", zt);
		}
		sql.append(" group by zqid order by convert(t1.zqid,signed) ");
		sql.append(" limit :start,:limit");
		paramMap.put("start", start);
		paramMap.put("limit", limit);
		logger.info("queryBxxList sql is " + sql.toString());
		logger.info("queryBxxList paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}
	@SuppressWarnings("deprecation")
	@Override
	public int queryBxxcount(JSONObject params) {
		String zqid=params.optString("zqid");
		String xmt=params.optString("xmt");
		String zt=params.optString("zt");
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from (select t1.ZQID from t_zhfw_tzrxx t1, t_zhfw_zqzrlb t2");
		sql.append("  where t2.HK_TYPE = '02' and t1.ZQID = t2.ID ");
		Map<String, Object> paramMap = new HashMap<>();
		if(StringUtils.isNotEmpty(zqid)){
			sql.append(" AND t1.ZQID = :zqid ");
			paramMap.put("zqid", zqid);
		}
		if(StringUtils.isNotEmpty(xmt)){
			sql.append(" AND t2.XM_TYPE = :xmt ");
			paramMap.put("xmt", xmt);
		}
		if(StringUtils.isNotEmpty(zt)){
			sql.append(" AND t2.ZT = :zt ");
			paramMap.put("zt", zt);
		}
		sql.append(" group by zqid) t ");
		logger.info("queryBxxcount sql is " + sql.toString());
		logger.info("queryBxxcount paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

	

}
